# @Time:   2021/01/06 14:05
# @Author: Zong Wei

import os
import time
import unittest
from ui_automation_testing.utils.SendMail import SendMail


from ui_automation_testing.utils.config import REPORT_PATH, CASES_PATH
from ui_automation_testing.utils.HTMLTestRunner_cn import HTMLTestRunner

# from utils.config import REPORT_PATH, CASES_PATH
# from utils.HTMLTestRunner_cn import HTMLTestRunner


def run_cases(executed_cases='test_*.py'):
    ui_cases_dir = os.path.join(CASES_PATH, "finance_zw")  # 运行指定文件夹的用例
    # ui_cases_dir = CASES_PATH  # 运行所有testcases
    # ui_cases_dir = file_dir+'\\ui_automation_testing\\test\\test_cases'

    file_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)))
    print(ui_cases_dir)
    case_list = []
    for dirpath, dirname, filename in os.walk(ui_cases_dir):    # os.walk查找顺序路径、目录、文件，变量名任意
        for file in filename:
            if file.endswith('.py') and not file.startswith('__'):
                case_list.append(file)

    discover = unittest.defaultTestLoader.discover(ui_cases_dir, pattern=executed_cases, top_level_dir=None)
    current_time = time.strftime('_%Y-%m-%d_%H%M%S')
    report_file = file_dir+'\\'+'report'+'\\' + 'Testing Report' + current_time + '.html'
    with open(report_file, 'wb') as fb:
        runner = HTMLTestRunner(stream=fb, title='UI Automation Testing Report', verbosity=1)
        runner.run(discover)

    # # 发送邮件
    # mail = SendMail()
    # mail.build_email(report_file=report_file)


if __name__ == '__main__':
    run_cases()
